package com.woniuxy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
 * @Author: ideaAdmin
 * TODO:
 * Date: 2022/2/8:17:54
 * Description:
 */
@Controller
@RequestMapping("/code")
public class CodeController {


    // 通过该方法得到验证码的图片
    @RequestMapping("/generate")
    private void generate(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 1.设置响应头
        // 设置数据的过期时间
        response.setDateHeader("Exprires", -1);

        // 设置是否缓存数据
        response.setHeader("Cache-Control", "no-cache");

        // 设置是否缓存参数
        response.setHeader("Pragma", "no-cache");

        // 2.设置contentType
        response.setContentType("imge/jpeg;charset:utf-8");

        // 3.创建图片
        int width = 1000;
        int height = 300;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);

        // 4.先获得图片的画笔
        Graphics graphics = image.getGraphics();

        // 5.画背景颜色
        graphics.setColor(Color.white);
        graphics.fillRect(0, 0, width, height);

        // 6.画数字、字母
        String code = "";
        // 7 画干扰点
        for (int i = 0; i < 50; i++) {
            Random random = new Random();
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            graphics.setColor(randomColor());
            graphics.fillOval(x, y, 20, 20);
        }

        // 8 画干扰线
        for (int i = 0; i < 50; i++) {
            Random random = new Random();
            int x1 = random.nextInt(width);
            int y1 = random.nextInt(height);
            int x2 = random.nextInt(width);
            int y2 = random.nextInt(height);
            graphics.setColor(randomColor());
            graphics.drawLine(x1, y1, x2, y2);
        }

        for (int i = 0; i < 5; i++) {
            // 随机生成字符
            String str = random();
            // 随机生成当前颜色
            Color color = randomColor();
            // 设置画笔颜色
            graphics.setColor(color);
            // 随机生成字体
            graphics.setFont(randomFont());
            // 将字符画到图片上
            graphics.drawString(str, i * 200, 200);
            // 获取验证码
            code += str;
        }
        //将验证码放到session中
        request.getSession().setAttribute("code", code);


        // 7.返回图片给浏览器
        ImageIO.write(image, "jpg", response.getOutputStream());

    }

    //随机字体
    private Font randomFont() {
        Random random = new Random();
        // 字体名字
        String[] font_names = new String[] { "Broadway", "方正姚体", "Footlight MT Light", "Sitka Text", "方正舒体", "幼圆",
                "Colonna MT" };
        // 字体风格
        int[] font_styles = new int[] { Font.BOLD, Font.ITALIC, Font.BOLD | Font.ITALIC };

        // 随机下标得到随机字体
        int name_index = random.nextInt(font_names.length);
        int style_index = random.nextInt(font_styles.length);
        // 随机字体
        return new Font(font_names[name_index], font_styles[style_index], 100 + random.nextInt(100));
    }

    private Color randomColor() {
        Random random = new Random();
        return new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
    }

    // 方法返回随机字符
    private String random() {
        int num = new Random().nextInt(62);

        // 0-9
        if (num >= 0 && num <= 9) {
            return num + "";
        }

        // A-Z
        if (num >= 10 && num <= 35) {
            return (char) (num + 55) + "";
        }

        // a-z
        if (num >= 36) {
            return (char) (num + 61) + "";
        }
        return "";
    }
}
